package com.cloud.bssp.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @description： TODO
 * @author：weirx
 * @date：2021/7/20 17:28
 * @version：3.0
 */
public class TestCopyFileByIO {

    //IO方法实现文件拷贝
    private static void traditionalCopy(String sourcePath, String destPath) throws Exception {
        File source = new File(sourcePath);
        File dest = new File(destPath);
        if (!dest.exists()) {
            dest.createNewFile();
        }
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(dest);
        byte[] buf = new byte[1024 * 1024];
        int len = 0;
        while ((len = fis.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        fis.close();
        fos.close();
    }

    public static void main(String[] args) throws Exception{
        long start = System.currentTimeMillis();
        traditionalCopy("E:\\workspace\\workspace.zip", "E:\\workspace\\workspace-" + System.currentTimeMillis() + "-bak.zip");
        long end = System.currentTimeMillis();
        System.out.println("用时为：" + (end-start));
    }
}
